﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebApplication3
{
    /// <summary>
    /// 连接SQL SERVER通用类
    /// </summary>
    public static class SqlHelper
    {
       
        //连接字符串
        private static string connString = "server=192.168.137.1;database=AnJi;pooling=false;User Id=sa;Password=110607";
        #region 不带参数的Sql语句
        /// <summary>
        /// 增删改
        /// </summary>
        /// <param name="sql"></param>
        /// <returns>返回值是受影响的行数,int类型</returns>
        public static int Update(string sql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 获取单个结果(第一行第一列)
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static object GetOneResult(string sql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                return cmd.ExecuteScalar();

            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 获取多个结果集
        /// </summary>
        /// <param name="sql">标准的SQL语句</param>
        /// <returns></returns>
        public static SqlDataReader GetReader(string sql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 非连接模型方法
        /// </summary>
        /// <param name="sql">标准的SQL语句</param>
        /// <returns></returns>
        public static DataSet GetDataSet(string sql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter sa = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
                sa.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 获取服务器当前时间
        /// </summary>
        /// <returns></returns>
        public static DateTime GetLogTime()
        {
            string sql = "select GETDATE()";
            try
            {
                return Convert.ToDateTime(GetOneResult(sql));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 带参数的Sql语句
        /// <summary>
        /// 增删改
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int Update(string sql, SqlParameter[] para)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                if (para != null)
                {
                    cmd.Parameters.AddRange(para);
                }
                return cmd.ExecuteNonQuery();//返回值是受影响的行数,int类型
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 获取单个结果(第一行第一列)
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static object GetOneResult(string sql, SqlParameter[] para)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                if (para != null)
                {
                    cmd.Parameters.AddRange(para);
                }
                return cmd.ExecuteScalar();

            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 获取多个结果集
        /// </summary>
        /// <param name="sql">标准的SQL语句</param>
        /// <returns></returns>
        public static SqlDataReader GetReader(string sql, SqlParameter[] para)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                if (para != null)
                {
                    cmd.Parameters.AddRange(para);
                }
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 非连接模型方法
        /// </summary>
        /// <param name="sql">标准的SQL语句</param>
        /// <returns></returns>
        public static DataSet GetDataSet(string sql, SqlParameter[] para)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter sa = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
                if (para != null)
                {
                    cmd.Parameters.AddRange(para);
                }
                sa.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion

        #region 使用存储过程执行
        /// <summary>
        /// 调用存储过程实现增删改
        /// </summary>
        /// <param name="procedureName">存储过程名称</param>
        /// <param name="para">sql参数</param>
        /// <returns></returns>
        public static int UpdateByProcedure(string procedureName, SqlParameter[] para)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = procedureName;
            try
            {
                conn.Open();
                if (para != null)
                {
                    cmd.Parameters.AddRange(para);
                }
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 调用存储过程获得单个结果
        /// </summary>
        /// <param name="procedureName"> 存储过程名称</param>
        /// <param name="para">SQL参数</param>
        /// <returns></returns>
        public static object GetOneByProcedure(string procedureName, SqlParameter[] para)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = procedureName;
            try
            {
                conn.Open();
                if (para != null)
                {
                    cmd.Parameters.AddRange(para);
                }
                return cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 调用存储过程获得多个结果集
        /// </summary>
        /// <param name="procedureName">存储过程名称</param>
        /// <param name="para">sql参数</param>
        /// <returns></returns>
        public static SqlDataReader GetReaderByProcedure(string procedureName, SqlParameter[] para)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = procedureName;
            try
            {
                conn.Open();
                if (para != null)
                {
                    cmd.Parameters.AddRange(para);
                }
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        #endregion

        #region 调用事务
        public static bool UpdateByTran(List<String> sqlList)
        {
            int TX = 0;
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            try
            {
                conn.Open();
                //开启事务
                cmd.Transaction = conn.BeginTransaction();
                //遍历所有sql语句,依次执行
                foreach (string item in sqlList)
                {
                    TX++;
                    cmd.CommandText = item;
                    cmd.ExecuteNonQuery();
                }
                //如果全部执行完毕,提交事务
                cmd.Transaction.Commit();
                return true;
            }
            catch (Exception ex)
            {
                //判断是不是由于事务运行导致,如果是,就回滚事务
                if (cmd.Transaction != null)
                {
                    cmd.Transaction.Rollback();//回滚事务
                    return false;
                }
                throw ex;
            }
            finally
            {
                if (cmd.Transaction != null)
                {
                    cmd.Transaction = null;//释放事务资源
                }
                conn.Close();//关闭连接
            }
        }
        #endregion
    }
}
